package com.yitongmao.adapter;

import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.yitongmao.dialog.BaseFlowLayout;
import com.yitongmao.dialog.DateModle;
import com.yitongmao.yitmfruit.R;

import java.util.List;

public class ComListviewAdapter extends BaseAdapter {

    private Context context;
    private Handler mHandler;
    private BaseFlowLayout mFlowLayout;
    private DateModle dateModle;
    private List<DateModle.GoodsTypesBean.AttrListBean> lables;
    private Bundle bundle;
    private int SELCT_SUCCESS = 0x00123;

    public ComListviewAdapter(DateModle dateModle, Context context, Handler mHandler) {
        this.dateModle = dateModle;
        this.context = context;
        this.mHandler = mHandler;
    }

    @Override
    public int getCount() {
        return dateModle.getGoods_types().size();
    }

    @Override
    public Object getItem(int i) {
        return null;
    }

    @Override
    public long getItemId(int i) {
        return 0;
    }

    @Override
    public View getView(int position, View view, ViewGroup viewGroup) {
        view = View.inflate(context,R.layout.emp_dialog_item, null);
        if (view != null){
            TextView type_txt = (TextView) view.findViewById(R.id.type_txt);
            String type = (String) dateModle.getGoods_types().get(position).getAttr_name()+" :";
           // type_txt.setText(type);
            type_txt.setTextColor(Color.parseColor("#a1a1a1"));
            mFlowLayout = (BaseFlowLayout) view.findViewById(R.id.news_top_title_base_flowlayout);
            lables = dateModle.getGoods_types().get(position).getAttr_list();
            if (mFlowLayout.getChildCount() == 0){
                TextView[]  textViews = new TextView[lables.size()];
                String[] type_url = new String[lables.size()];
                for (int i = 0; i < lables.size(); i++) {
                    TextView tv = (TextView) LayoutInflater.from(context).inflate(R.layout.news_top_title_search_, mFlowLayout, false);
                    textViews[i] = tv;
                    if (i == 0){
                        textViews[i].setBackgroundResource(R.drawable.news_top_title_search_label_bg);
                        textViews[i].setTextColor(Color.parseColor("#FFFFFF"));
                    }else {
                        textViews[i].setBackgroundResource(R.drawable.news_top_title_search_bg);
                        textViews[i].setTextColor(Color.parseColor("#535353"));
                    }
                    textViews[i].setText(lables.get(i).getAttr_value());
                    textViews[i].setTag(i);
                    type_url[i] = lables.get(i).getImg_url();   //对应的id
                    mFlowLayout.addView(textViews[i]);//添加到父View
                }
                //绑定标签的Click事件
                for(int j = 0 ; j < textViews.length;j++){
                    textViews[j].setTag(textViews);
                    textViews[j].setOnClickListener(new LableClickListener(type,type_url));
                }
            }
        }
        return view;
    }

    class LableClickListener implements View.OnClickListener {
        private String type;
        private String[] type_url;
        public LableClickListener(String type,String[] type_url){
            this.type = type;
            this.type_url = type_url;
        }
        @Override
        public void onClick(View v) {
            TextView[] textViews = (TextView[]) v.getTag();
            TextView tv = (TextView) v;
            for (int i = 0; i < textViews.length; i++) {
                //让点击的标签背景变成橙色，字体颜色变为白色
                if (tv.equals(textViews[i])) {
                    textViews[i].setBackgroundResource(R.drawable.news_top_title_search_label_bg);
                    textViews[i].setTextColor(Color.parseColor("#FFFFFF"));
                    //传递属性选择后的商品数据
                    Message message = new Message();
                    bundle = new Bundle();
                    bundle.putString("type",type);
                    bundle.putString("type_name",tv.getText().toString());
                    bundle.putString("type_url",type_url[i]);
                    message.setData(bundle);
                    message.what = SELCT_SUCCESS;
                    mHandler.sendMessage(message);
                } else {
                    //其他标签背景变成白色，字体颜色为黑色
                    textViews[i].setBackgroundResource(R.drawable.news_top_title_search_bg);
                    textViews[i].setTextColor(Color.parseColor("#535353"));
                }
            }
        }

    }

}
